package passengers.demo;
import java.util.regex.Pattern;

import presence.demo.R;
import database.demo.PassengersOperations;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.CursorIndexOutOfBoundsException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 * saving passenger's details
 */
public class NewPassMainActivity extends Activity {
	private PassengersOperations passengerDBoperation;
	TextView tmpTextID;
	EditText tmpLastName;
	EditText tmpFirstName;
	EditText tmpPhone;
	EditText tmpEmail;
	public final Pattern EMAIL_ADDRESS_PATTERN = Pattern.compile(
            "[a-zA-Z0-9+._%-+]{1,256}" +
            "@" +
            "[a-zA-Z0-9][a-zA-Z0-9-]{0,64}" +
            "(" +
            "." +
            "[a-zA-Z0-9][a-zA-Z0-9-]{0,25}" +
            ")+"
    );//end of pattern
	
	//when screen loads
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.new_pass);	
		passengerDBoperation = new PassengersOperations(this);
		passengerDBoperation.open();
		getMaxColumnData();
		addButtonListeners();
		findViewById(R.id.lastText).requestFocus();
	}//end of onCreate
	
	//get the next available ID
	public void getMaxColumnData() {
		tmpTextID=(TextView) findViewById(R.id.idTextt);
		Cursor cursor = passengerDBoperation.getMaxID();
		if(cursor.getCount()>0){
			int tmpID= Integer.parseInt(cursor.getString(0));
			if (tmpID>=1){
				tmpID++;
				tmpTextID.setText(tmpID+"");
			}
		}
		else tmpTextID.setText("1");
	}//end of getMaxColumnData
	
	//button actions
	public void addButtonListeners() {	 
		Button addButtton = (Button) findViewById(R.id.btnAddPass);
		tmpTextID=(TextView) findViewById(R.id.idTextt);
		tmpLastName = (EditText) findViewById(R.id.lastText);
		tmpFirstName = (EditText) findViewById(R.id.textFirst);
		tmpPhone = (EditText) findViewById(R.id.phoneText);
		tmpEmail = (EditText) findViewById(R.id.mailText);
		addButtton.setOnClickListener(new OnClickListener() {
		@Override
		public void onClick(View view) {
			int check = 0;
			if(tmpLastName.getText().toString().length()==0)
			{
				check = 1;
			}
			else if(tmpFirstName.getText().toString().length()==0)
			{
				check = 2;
			}
			//else if(checkPhone(tmpPhone.getText().toString()))
			//{
				//check = 3;
			//}
			else if(!(checkEmail(tmpEmail.getText().toString())))
            {
				check = 4;
            }
           
			
			switch (check){
			case 0:
				addUser(tmpTextID,tmpLastName,tmpFirstName,tmpPhone,tmpEmail);	
				break;
			case 1:
				Toast.makeText(NewPassMainActivity.this,"Please enter passenger last name", Toast.LENGTH_LONG).show();
				break;
			case 2:
				Toast.makeText(NewPassMainActivity.this,"Please enter passenger first name", Toast.LENGTH_LONG).show();
				break;
			case 3:
				Toast.makeText(NewPassMainActivity.this,"Phone already exsists", Toast.LENGTH_LONG).show();
				break;
			case 4:
				Toast.makeText(NewPassMainActivity.this,"Invalid Email Addresss", Toast.LENGTH_LONG).show();
				break;
			}
		}
		}); 		
	}//end of addButtonListeners
	
	//checking pattern for email
	private boolean checkEmail(String email) {
		if(email.length()==0){
			return true;
		}
		else return EMAIL_ADDRESS_PATTERN.matcher(email).matches();
    }//end of checkEmail
	
	//check if this phone already exists
	private boolean checkPhone(String phone) {
		int i;
		tmpTextID=(TextView) findViewById(R.id.idTextt);
		for (i=1;i<Integer.parseInt(tmpTextID.getText().toString());i++){
			Cursor cursor = passengerDBoperation.getPass(i+"");
		    String tmpPhone = cursor.getString(3);
		    if (phone.equals(tmpPhone)){
		    	return true;
		    }
		}
	 return false;   
	}//end of checkPhone
	
	//save new user
	public void addUser(TextView tmpID,EditText tmpLastName, EditText tmpFirstName, EditText tmpPhone, EditText tmpEmail) {
		if(passengerDBoperation.addPassenger(tmpID.getText().toString(), tmpLastName.getText().toString(),
				tmpFirstName.getText().toString(), tmpPhone.getText().toString(),tmpEmail.getText().toString())==null)
		{
			Toast.makeText(NewPassMainActivity.this,"ID already exists!", Toast.LENGTH_LONG).show();
		}
		else{
		
		AlertDialog alertDialog = new AlertDialog.Builder(this).create();
		alertDialog.setTitle(":)");
		alertDialog.setMessage(tmpLastName.getText().toString()+" "+tmpFirstName.getText().toString()+" added successfully! ");
		alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
		public void onClick(DialogInterface dialog, int which) {
			NewPassMainActivity.this.finish();
			}
		});
		alertDialog.setIcon(R.drawable.ic_launcher);
		alertDialog.show();
		}
	}//end of addUser
}












